package com.aaa.web.service.impl.employeeserviceimpl;

import com.aaa.entity.BackRole;
import com.aaa.entity.Per;
import com.aaa.util.EmpUtil;
import com.aaa.web.commons.EmpQuery;
import com.aaa.web.commons.Result;
import com.aaa.web.dao.iemployeedao.IEmployeeDao;
import com.aaa.entity.Employee;
import com.aaa.web.service.iemployeeservice.IEmployeeService;
import com.aaa.web.shiro.ShiroUtil;
import com.aaa.web.systemconst.ISystemConst;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yyh
 * @Description //员工页面操作
 * @Date 2018/12/26 9:47 
 * @Param 
 * @return 
 **/
@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private IEmployeeDao employeeDao;

    /**
     * @Author yyh
     * @Description //根据搜索条件获取员工的信息
     * @Date 2018/12/26 9:43 
     * @Param [query, id]
     * @return java.util.Map
     **/
    @Override
    public Map getAllEmployee(EmpQuery query,Integer id) {
        Integer schoolid=id;
        if(id==null||id==0){
            schoolid=EmpUtil.getEmp().getSchoolid();
        }
        int page = query.getPage();
        int size = query.getLimit();
        String telephone = query.getTelephone();
        PageHelper.startPage(page,size);
        Map m = new HashMap();
        Map map = new HashMap();
        map.put("schoolid",schoolid);
        map.put("telephone",telephone);
        m.put("data",employeeDao.getAllEmployee(map));
        m.put("count",employeeDao.getEmployeeCount(map));
        m.put("code",0);
        m.put("msg","");
        return m;
    }

    /**
     * @Author yyh
     * @Description //根据员工employeeid获取员工的信息
     * @Date 2018/12/26 9:43 
     * @Param [telephone]
     * @return com.aaa.entity.Employee
     **/
    @Override
    public Employee getEmployeeById(String telephone) {
        return employeeDao.getEmployeeById(telephone);
    }
    /**
     * @Author yyh
     * @Description //获取所有的角色,并将该员工拥有的角色选中
     * @Date 2018/12/26 9:44 
     * @Param [id]
     * @return java.util.List<com.aaa.entity.Per>
     **/
    @Override
    public List<Per>  getAllRole(Integer id) {
        List<Per> list = employeeDao.getAllRole();
        List<Per> check = employeeDao.getEmployeeRoleById(id);
        if(EmpUtil.getEmp()!=null){
            Integer schoolid = EmpUtil.getEmp().getSchoolid();
            for (int i = 0; i < list.size(); i++) {
                if("1".equals(list.get(i).getValue())&&schoolid!=0){
                    list.remove(i);
                }
                for (int j = 0; j < check.size() ; j++) {
                    if(check.get(j).getValue().equals(list.get(i).getValue())){
                        list.get(i).setChecked(true);
                    }
                }
            }
        }else{
            return null;
        }
        return list;
    }
    /**
     * @Author yyh
     * @Description //更新员工的角色信息
     * @Date 2018/12/26 9:45 
     * @Param [array, id]
     * @return com.aaa.web.commons.Result
     **/
    @Override
    public Result updateRole(int[] array, Integer id) {
        employeeDao.deleteRole(id);
        Map map = new HashMap();
        Result result = new Result();
        map.put("employeeid",id);
        if(array==null){

        }else {
            for (int j = 0; j < array.length; j++) {
                map.put("roleid",array[j]);
                int n = employeeDao.insertRole(map);
                if(n<1){
                    result.setCode(ISystemConst.DEFEAT_CODE);
                    return result;
                }
            }
        }
        result.setCode(ISystemConst.SUCCESS_CODE);
        return result;

    }
    /**
     * @Author yyh
     * @Description //更新员工的基本信息
     * @Date 2018/12/26 9:45 
     * @Param [emp]
     * @return com.aaa.web.commons.Result
     **/
    @Override
    public Result updateEmp(Employee emp) {
        Employee employee = emp;
        String password = ShiroUtil.getPassword(emp.getTelephone(),emp.getPassword());
        employee.setPassword(password);
        Integer flag =employeeDao.updateEmp(emp);
        Result result = new Result();
        if(flag>0){
            result.setCode(ISystemConst.SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.DEFEAT_CODE);
        }
        return result;
    }

    @Override
    public Result updateEmpState(Employee emp) {
        Integer flag =employeeDao.updateEmpState(emp);
        Result result = new Result();
        if(flag>0){
            result.setCode(ISystemConst.SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.DEFEAT_CODE);
        }
        return result;
    }

    /**
     * @Author yyh
     * @Description //添加新员工
     * @Date 2018/12/26 9:46 
     * @Param [emp]
     * @return com.aaa.web.commons.Result
     **/
    @Override
    public Result addEmp(Employee emp) {
        Employee employee = emp;
        Result result = new Result();
        Employee employeeById= employeeDao.getEmployeeById(emp.getTelephone());
        if(employeeById!=null){
            result.setCode(ISystemConst.DISABLE_CODE);
            return result;
        }
        if(employee.getSchoolid() == null){
            employee.setSchoolid(EmpUtil.getEmp().getSchoolid());
        }
        String password = ShiroUtil.getPassword(emp.getTelephone(),emp.getPassword());
        employee.setPassword(password);
        Integer flag = employeeDao.addEmp(emp);

        if(flag>0){
            result.setCode(ISystemConst.SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.DEFEAT_CODE);
        }
        return result;
    }


}
